package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.PageDTO;
import com.sky.dto.QueryOrderDetailsDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.user.OrderDetailMapper;
import com.sky.mapper.user.OrderMapper;
import com.sky.service.OrderDetailService;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 根据订单id查询订单详情
     * @param id
     * @return
     */
    @Override
    public OrderVO details(long id) {
        // 查询订单基本信息
        Orders order = orderMapper.selectById(id);
        // 查询订单详情
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderId, order.getId());
        List<OrderDetail> orderDetailList = orderDetailMapper.selectList(wrapper);
        // 封装 OrderVo
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);
        orderVO.setOrderDetailList(orderDetailList);

        return orderVO;
    }

    /**
     * 查询历史订单
     * @param query
     * @return
     */
    @Override
    public PageDTO<OrderVO> query(QueryOrderDetailsDTO query) {

        // 查询历史订单
        Page<Orders> page = Page.of(query.getPage(), query.getPageSize());
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId())
                .eq(null != query.getStatus(), Orders::getStatus, query.getStatus());
        orderMapper.selectPage(page, wrapper);

        ArrayList<OrderVO> list = new ArrayList<>();

        // 查询订单明细
        if(page.getTotal() > 0){
            for (Orders orders : page.getRecords()) {
                // 设置查询条件
                LambdaQueryWrapper<OrderDetail> wrapperOrderId = new LambdaQueryWrapper<>();
                wrapperOrderId.eq(OrderDetail::getOrderId, orders.getId());

                List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapperOrderId);

                OrderVO orderVO = new OrderVO();
                BeanUtils.copyProperties(orders, orderVO);
                orderVO.setOrderDetailList(orderDetails);

                list.add(orderVO);
            }
        }else{
            return new PageDTO<>(0, null);
        }

        return new PageDTO<OrderVO>(page.getTotal(), list);
    }


}
